package com.msconfig.mvp.view.adapter;

/*
 * author: caojunhao
 * date  : 2020/5/11
 * desc  :
 */

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.geo.msconfig.R;
import com.msconfig.bean.ConfigInfoBean;
import com.msconfig.event.PowerOnDataDialogDismissEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashSet;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.geo.msconfig.R;
import com.msconfig.event.PowerOnDataDialogDismissEvent;
import com.msconfig.mvp.view.adapter.TimeZoneAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashSet;

public class ConfigAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<ConfigInfoBean> mList;
    public boolean flage = false;//是否显示checkbox
    private int mCurrentItem=0;
    private boolean mIsClick =false;

    public ConfigAdapter(Context context, ArrayList<ConfigInfoBean> list){
        mContext= context;
        mList=list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        //getCheckedItemIds()方法要求此处返回为真
        return true;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            convertView= LayoutInflater.from(mContext).inflate(R.layout.layout_fragment_config_item,parent,false);
            viewHolder=new ViewHolder();
            viewHolder.tvFileName=(TextView) convertView.findViewById(R.id.tv_filename);
            viewHolder.tvTime=(TextView) convertView.findViewById(R.id.tv_time);
            viewHolder.checkBox=(CheckBox) convertView.findViewById(R.id.checkbox);
            viewHolder.parent=(RelativeLayout) convertView.findViewById(R.id.parent);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tvFileName.setText(mList.get(position).getFileName());
        viewHolder.tvTime.setText(mList.get(position).getTime());


        // 根据isSelected来设置checkbox的显示状况
        if (flage) {
            viewHolder.checkBox.setVisibility(View.VISIBLE);
        } else {
            viewHolder.checkBox.setVisibility(View.GONE);
        }

        viewHolder.parent.setBackgroundColor(ContextCompat.getColor(mContext.getApplicationContext(),R.color.white));
        if (mCurrentItem==position&& mIsClick){
            viewHolder.parent.setBackgroundColor(ContextCompat.getColor(mContext.getApplicationContext(),R.color.green));
        }

        viewHolder.checkBox.setChecked(mList.get(position).isCheck);


        return convertView;
    }

    public class ViewHolder{
        TextView tvFileName;
        TextView tvTime;
        CheckBox checkBox;
        RelativeLayout parent;
    }

    public void setCurrentItem(int currentItem){
        this.mCurrentItem=currentItem;
    }

    public void setmIsClick(boolean mIsClick){
        this.mIsClick = mIsClick;
    }

}


